'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2012, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl, Simple Redirect, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'
' 

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Framework.Providers
Imports Microsoft.ApplicationBlocks.Data
Imports System
Imports System.Data
Imports System.Data.SqlClient

NameSpace WillStrohl.Modules.SimpleRedirect

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' SQL Server implementation of the abstract DataProvider class
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class SqlDataProvider
        Inherits DataProvider
        
#Region " Private Members "

        Private Const PROVIDER_TYPE As String = "data"
        Private Const MODULE_QUALIFIER As String = "wns_simpleredirect_"

        Private p_providerConfiguration As ProviderConfiguration = ProviderConfiguration.GetProviderConfiguration(PROVIDER_TYPE)
        Private p_connectionString As String = String.Empty
        Private p_providerPath As String = String.Empty
        Private p_objectQualifier As String = String.Empty
        Private p_databaseOwner As String = String.Empty

        Private Const METHOD_ADDREDIRECT As String = "AddRedirect"
        Private Const METHOD_GETREDIRECT As String = "GetRedirect"
        Private Const METHOD_GETREDIRECTBYURL As String = "GetRedirectByUrl"
        Private Const METHOD_GETREDIRECTS As String = "GetRedirects"
        Private Const METHOD_UPDATEREDIRECT As String = "UpdateRedirect"
        Private Const METHOD_DELETEREDIRECT As String = "DeleteRedirect"
        Private Const METHOD_ADDVISITOR As String = "AddVisitor"
        Private Const METHOD_CLEARVISITORLOG As String = "ClearVisitorLog"
        Private Const METHOD_DELETEVISITORSBYMODULE As String = "DeleteVisitorsByModule"
        Private Const METHOD_DELETEVISITORSBYREDIRECT As String = "DeleteVisitorsByRedirect"
        Private Const METHOD_GETDOMAINSBYREDIRECT As String = "GetDomainsByRedirect"
        Private Const METHOD_GETREDIRECTSREVERSE As String = "GetRedirectsReverse"
        Private Const METHOD_GETREDIRECTVISITCOUNT As String = "GetRedirectVisitCount"
        Private Const METHOD_GETURLSBYREDIRECT As String = "GetUrlsByRedirect"

#End Region

#Region " Constructors "

        Public Sub New()

            ' Read the configuration specific information for this provider
            Dim objProvider As Provider = CType(p_providerConfiguration.Providers(p_providerConfiguration.DefaultProvider), Provider)

            ' Read the attributes for this provider

            'Get Connection string from web.config
            p_connectionString = Config.GetConnectionString()

            If String.IsNullOrEmpty(p_connectionString) Then
                ' Use connection string specified in provider
                p_connectionString = objProvider.Attributes("connectionString")
            End If

            p_providerPath = objProvider.Attributes("providerPath")
            p_objectQualifier = objProvider.Attributes("objectQualifier")

            If Not String.IsNullOrEmpty(p_objectQualifier) And p_objectQualifier.EndsWith("_") = False Then
                p_objectQualifier = String.Concat(p_objectQualifier, "_")
            End If

            p_databaseOwner = objProvider.Attributes("databaseOwner")
            If Not String.IsNullOrEmpty(p_databaseOwner) And p_databaseOwner.EndsWith(".") = False Then
                p_databaseOwner = String.Concat(p_databaseOwner, ".")
            End If

        End Sub

#End Region

#Region " Properties "

        Public ReadOnly Property ConnectionString() As String
            Get
                Return p_connectionString
            End Get
        End Property

        Public ReadOnly Property ProviderPath() As String
            Get
                Return p_providerPath
            End Get
        End Property

        Public ReadOnly Property ObjectQualifier() As String
            Get
                Return p_objectQualifier
            End Get
        End Property

        Public ReadOnly Property DatabaseOwner() As String
            Get
                Return p_databaseOwner
            End Get
        End Property

        Private ReadOnly Property NamePrefix() As String
            Get
                Return String.Concat(p_databaseOwner, p_objectQualifier, MODULE_QUALIFIER)
            End Get
        End Property

#End Region

#Region " Private Methods "

        Private Function GetFullyQualifiedName(ByVal name As String) As String
            Return String.Concat(NamePrefix, name)
        End Function

        Private Function GetNull(ByVal Field As Object) As Object
            Return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value)
        End Function

#End Region

#Region " Public Methods "

        Public Overrides Function AddRedirect(ModuleId As Integer, RedirectFrom As String, RedirectTo As String) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName(METHOD_ADDREDIRECT), ModuleId, GetNull(RedirectFrom), GetNull(RedirectTo)), Integer)
        End Function

        Public Overrides Function GetRedirect(RedirectId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(METHOD_GETREDIRECT), RedirectId), IDataReader)
        End Function

        Public Overrides Function GetRedirectByUrl(RedirectTo As String) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(METHOD_GETREDIRECTBYURL), RedirectTo), IDataReader)
        End Function

        Public Overrides Function GetRedirects(ModuleId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(METHOD_GETREDIRECTS), ModuleId), IDataReader)
        End Function

        Public Overrides Sub UpdateRedirect(RedirectId As Integer, ModuleId As Integer, RedirectFrom As String, RedirectTo As String)
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(METHOD_UPDATEREDIRECT), RedirectId, ModuleId, GetNull(RedirectFrom), GetNull(RedirectTo))
        End Sub

        Public Overrides Sub DeleteRedirect(RedirectId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(METHOD_DELETEREDIRECT), RedirectId)
        End Sub

        Public Overrides Function AddVisitor(RedirectId As Integer, ReferringUrl As String, ReferringDomain As String) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName(METHOD_ADDVISITOR), RedirectId, GetNull(ReferringUrl), GetNull(ReferringDomain)), Integer)
        End Function

        Public Overrides Sub ClearVisitorLog()
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(METHOD_CLEARVISITORLOG))
        End Sub

        Public Overrides Sub DeleteVisitorsByModule(ModuleId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(METHOD_DELETEVISITORSBYMODULE), ModuleId)
        End Sub

        Public Overrides Sub DeleteVisitorsByRedirect(RedirectId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(METHOD_DELETEVISITORSBYREDIRECT), RedirectId)
        End Sub

        Public Overrides Function GetDomainsByRedirect(RedirectId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(METHOD_GETDOMAINSBYREDIRECT), RedirectId), IDataReader)
        End Function

        Public Overrides Function GetRedirectsReverse(ModuleId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(METHOD_GETREDIRECTSREVERSE), ModuleId), IDataReader)
        End Function

        Public Overrides Function GetRedirectVisitCount(RedirectId As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName(METHOD_GETREDIRECTVISITCOUNT), RedirectId), Integer)
        End Function

        Public Overrides Function GetUrlsByRedirect(RedirectId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(METHOD_GETURLSBYREDIRECT), RedirectId), IDataReader)
        End Function

#End Region
        
    End Class

End Namespace